/**
 * This file is part of 1genia trampoline
 * Copyright (C) 2007-2008 1genia (contact@1genia.com)
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; version 3 of the License. 
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; see the file COPYING.TXT.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301, USA. 
 **/
package com.genia.toolbox.web.gwt.basics.service;

import java.io.IOException;
import java.util.Locale;

import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.context.ApplicationContext;
import org.springframework.web.servlet.mvc.Controller;

import com.genia.toolbox.basics.exception.BundledException;
import com.genia.toolbox.basics.i18n.SupportedLanguage;
import com.genia.toolbox.spring.initializer.ApplicationContextHolder;
import com.genia.toolbox.spring.provider.message.manager.CustomResourceBundleMessageSource;
import com.genia.toolbox.web.gwt.basics.controller.GwtServiceController;

/**
 * Gwt servlet.
 */
@SuppressWarnings("serial")
public class FakeGwtService
    extends HttpServlet
{

  public FakeGwtService()
      throws BundledException
  {
    CustomResourceBundleMessageSource messageSource = (CustomResourceBundleMessageSource) getApplicationContext().getBean("messageSource");
    messageSource.addSupportedLanguage(new SupportedLanguage()
    {

      public Locale getLocale()
      {
        return Locale.ENGLISH;
      }



      public int getOrder()
      {
        return 1;
      }

    });
    messageSource.addSupportedLanguage(new SupportedLanguage()
    {

      public Locale getLocale()
      {
        return Locale.FRENCH;
      }



      public int getOrder()
      {
        return 2;
      }
    });
  }



  @Override
  public void service(ServletRequest request, ServletResponse response)
      throws ServletException, IOException
  {
    try {
      ((Controller) getApplicationContext().getBeansOfType(GwtServiceController.class).values().iterator().next()).handleRequest((HttpServletRequest) request, (HttpServletResponse) response);
    }
    catch (Exception e) {
      throw new ServletException(e);
    }
  }



  private ApplicationContext getApplicationContext()
      throws BundledException
  {
    return ApplicationContextHolder.getApplicationContext();
  }

}
